
#include "Stdafx.h"
#include "avatarManager.h"
#include "BaseEditor.h"
#include "NodeEditor.h"
#include "OgitorsRoot.h"
using namespace Ogre;
using namespace Ogitors;

template<> avatarManager*  Ogitors::Singleton<avatarManager>::ms_Singleton = 0;

avatarManager::avatarManager(    ) 
{
	 init();
	 //initAvatarResources();
	 //initAvatarResources("..\\Data\\PropertySheets\\AvatarResourcesMale.xml");
	 initAvatarDefaultConfig();

	 

}
std::vector<std::string> stringSplit(std::string source, std::string delimiter)
{
	std::vector<std::string> v;

	int pos = 0;
	while (1)
	{
		int p = source.find_first_of(delimiter, pos);
		if (p < 0)
		{
			v.push_back(source.substr(pos, -1));
			break;
		}
		v.push_back(source.substr(pos, p - pos));
		pos = p + 1;
	}
	return v;
}
// std::vector<int> getPartIndex(std::string sex, std::string part)
// {
// 	std::vector<int> v;
// 
// 	if (sex != "f" && sex != "m")
// 		return v;
// 
// 	if (part == "hands" || part == "hand")
// 	{
// 		v.push_back(E_PART_HANDS);
// 	}
// 	else if (part == "hair")
// 	{
// 		v.push_back(E_PART_HAIR);
// 	}
// 	else if (part == "coat")
// 	{
// 		v.push_back(E_PART_COAT);
// 	}
// 	else if (part == "pants" || part == "pant")
// 	{
// 		v.push_back(E_PART_PANTS);
// 	}
// 	else if (part == "shoes")
// 	{
// 		v.push_back(E_PART_SHOES);
// 	}
// 	else if(part == "sock")
// 	{
// 		v.push_back(E_PART_SOCKS);
// 	}
// 
// 	return v;
// }
// std::vector<int> getPartIndex(int sex, std::string part)
// {
// 	std::string str;
// 	if (sex == E_SEX_FEMALE)
// 		str = "f";
// 	else if (sex == E_SEX_MALE)
// 		str = "m";
// 	return getPartIndex(str, part);
// }


// void avatarManager::initAvatarResources(const std::string & config_path)
// {
// 	m_AvatarResources[0].clear();
// 	m_AvatarResources[1].clear();
// 
// 	m_PartLists[0].clear();
// 		m_PartLists[1].clear();
// 	std::vector<AvatarResourceInfo> suits;
// 	std::vector<AvatarResourceInfo> suitsmale;
// 
// 	TiXmlDocument doc(config_path.c_str());
// 	if (doc.LoadFile())
// 	{
// 		TiXmlElement *AvatarResources = doc.FirstChildElement("AvatarResources");
// 		Ogre::String sex =  AvatarResources->Attribute("sex");
// 
// 		m_currentSex = (SEXTYPE)Ogre::StringConverter::parseInt(sex);
// 
// 		for (TiXmlElement *sex = AvatarResources->FirstChildElement(); sex != 0; sex = sex->NextSiblingElement())
// 		{
// 			Ogre::String sexId = sex->Attribute("sexId");
// 			if (sexId == "0")
// 			{
// 				for (TiXmlElement *i =  sex->FirstChildElement(); i != 0; i = i->NextSiblingElement())
// 				{
// 					Ogre::String file = i->Attribute("File");
// 					Ogre::String name = i->Attribute("Name");
// 					Ogre::StringUtil::toLowerCase(file);
// 					Ogre::StringUtil::toLowerCase(name);
// 
// 					AvatarResourceInfo info;
// 					info.id = atoi(i->Attribute("Id"));
// 					info.file = i->Attribute("File");
// 					info.name = i->Attribute("Name");
// 					info.sex = atoi(i->Attribute("Sex"));
// 
// 					std::vector<std::string> v = stringSplit(i->Attribute("Part"), "_");
// 					for (std::vector<std::string>::iterator j = v.begin(); j != v.end(); j++)
// 						info.parts.push_back(atoi(j->c_str()));
// 
// 					m_AvatarResources[0][info.id] = info;
// 
// 					if (info.parts.size() > 1)
// 						suits.push_back(info);
// 					else
// 						m_PartLists[0][info.parts[0]].push_back(info);
// 				}
// 			}
// 			if (sexId == "1")
// 			{
// 				for (TiXmlElement *i =  sex->FirstChildElement(); i != 0; i = i->NextSiblingElement())
// 				{
// 					Ogre::String file = i->Attribute("File");
// 					Ogre::String name = i->Attribute("Name");
// 					Ogre::StringUtil::toLowerCase(file);
// 					Ogre::StringUtil::toLowerCase(name);
// 
// 					AvatarResourceInfo info;
// 					info.id = atoi(i->Attribute("Id"));
// 					info.file = i->Attribute("File");
// 					info.name = i->Attribute("Name");
// 					info.sex = atoi(i->Attribute("Sex"));
// 
// 					std::vector<std::string> v = stringSplit(i->Attribute("Part"), "_");
// 					for (std::vector<std::string>::iterator j = v.begin(); j != v.end(); j++)
// 						info.parts.push_back(atoi(j->c_str()));
// 
// 					m_AvatarResources[1][info.id] = info;
// 
// 					if (info.parts.size() > 1)
// 						suitsmale.push_back(info);
// 					else
// 						m_PartLists[1][info.parts[0]].push_back(info);
// 				}
// 			}
// 
// 			
// 		}
// 	}
// 
// 	for (std::vector<AvatarResourceInfo>::iterator i = suits.begin(); i != suits.end(); i++)
// 	{
// 		int partid = getPartIndex(i->sex, "coat")[0];
// 		m_PartLists[0][partid].push_back(*i);
// 	}
// 	for (std::vector<AvatarResourceInfo>::iterator i = suitsmale.begin(); i != suitsmale.end(); i++)
// 	{
// 		int partid = getPartIndex(i->sex, "coat")[0];
// 		m_PartLists[1][partid].push_back(*i);
// 	}
// }
    void avatarManager::init()
	{

		FindFileList( MHAIRNames, "..\\Media\\moonM\\hair\\*.mesh");//1
		FindFileList( MCOATNames, "..\\Media\\moonM\\coat\\*.mesh");//2
		FindFileList( MHANDSNames, "..\\Media\\moonM\\hand\\*.mesh");//3
		FindFileList( MPANTSNames, "..\\Media\\moonM\\pants\\*.mesh");//4
		FindFileList( MSHOESNames, "..\\Media\\moonM\\shose\\*.mesh");//5
		//FindFileList( MSOCKSNames, "..\\Media\\moonM\\shose\\*.mesh");//6
		FindFileList( MattactMesh, "..\\Media\\moonM\\attactMesh\\*.mesh");//6
		FindFileList( MAnimationSke, "..\\Media\\moonM\\animation\\*.skeleton");//6
 		FindFileList( MHEADNames, "..\\Media\\moonM\\head\\*.mesh");//3


		FindFileList( FOneMesh, "..\\Media\\moonF\\one\\*.mesh");//5
		FindFileList( FShockMesh, "..\\Media\\moonF\\shock\\*.mesh");//6
		FindFileList( FHAIRNames, "..\\Media\\moonF\\hair\\*.mesh");//1
		FindFileList( FCOATNames, "..\\Media\\moonF\\coat\\*.mesh");//2
		FindFileList( FHANDSNames, "..\\Media\\moonF\\hand\\*.mesh");//3
		FindFileList( FHEADNames, "..\\Media\\moonF\\head\\*.mesh");//3
		FindFileList( FPANTSNames, "..\\Media\\moonF\\pants\\*.mesh");//4
		FindFileList( FSHOESNames, "..\\Media\\moonF\\shose\\*.mesh");//5
		//FindFileList( FSOCKSNames, "..\\Media\\moonF\\sock\\*.mesh");//6
		FindFileList( FattactMesh, "..\\Media\\moonF\\attactMesh\\*.mesh");//6
		FindFileList( FAnimationSke, "..\\Media\\moonF\\animation\\*.skeleton");//6



		// 		FindFileList( SkeList, "..\\Media\\xingguangavatar\\animation\\*.skeleton");
// 		FindFileList( faceList, "..\\Media\\xingguangavatar\\faceAnimation\\*.skeleton");


	}


// std::string avatarManager::getModel()
// {
// 	return m_AvatarModels[m_currentSex];
// }
void avatarManager::initAvatarDefaultConfig(const std::string & config_path)
{
	TiXmlDocument doc(config_path.c_str());

	if (doc.LoadFile())
	{
		TiXmlNode *root = doc.FirstChild("AvatarDefault");

// 		for (TiXmlElement *i = root->FirstChildElement("Base")->FirstChildElement(); i != 0; i = i->NextSiblingElement())
// 			m_AvatarParts.push_back(i->Attribute("Name"));

// 		for (TiXmlElement *i = root->FirstChildElement("Actions")->FirstChildElement(); i != 0; i = i->NextSiblingElement())
// 			m_AvatarActions[i->Attribute("Type")] = i->Attribute("File");

		for (TiXmlElement *i = root->FirstChildElement("Avatar")->FirstChildElement(); i != 0; i = i->NextSiblingElement())
			m_AvatarModels[atoi(i->Attribute("Sex"))] = i->Attribute("File");

// 		m_AnimationParam.types["Blend"] = Ogre::StringConverter::parseReal(root->FirstChildElement("Actions")->Attribute("Blend"));
// 		m_AnimationParam.types["Fade"]  = Ogre::StringConverter::parseReal(root->FirstChildElement("Actions")->Attribute("Fade"));
	}
}

// 
// 
// void avatarManager::init(const char *cfg)
// {
// 	TiXmlDocument doc(cfg);
// 
// 	if (doc.LoadFile())
// 	{
// 		m_Steps.clear();
// 		for (TiXmlElement *i = doc.FirstChild("DanceResource")->FirstChildElement(); i != 0; i = i->NextSiblingElement())
// 		{
// 			DanceInfo info;
// 			info.id = Ogre::StringConverter::parseInt(i->Attribute("Id"));
// 			info.file = Ogre::String("F") +  i->Attribute("File");
// 			info.bpm = Ogre::StringConverter::parseReal(i->Attribute("Bpm"));
// 			info.reverse_key = 0;
// 
// 			const char *p = i->Attribute("Key");
// 			int len = strlen(p);
// 			int level = len;
// 			if (level == 10)
// 				info.reverse_key = Ogre::Math::RangeRandom(1, 9);// dancePlugin::getPluginPtr()->m_pRoot->rand(1, 9);
// 
// 			info.key.clear();
// 			len = __min(9, len);
// 			for (int i = 0; i < len; i++)
// 				info.key.push_back(p[i] - '0');
// 
// 			if (level < 6)
// 				info.round = 1;
// 			else if (level == 10)
// 				info.round = 4;
// 			else
// 				info.round = 2;
// 
// 			m_Steps.push_back(info);
// 		}
// 	}
// }
// 

